package com.ctsi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.common.util.Search;
import com.ctsi.core.common.entity.LoginUser;
import com.ctsi.core.common.util.$;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.system.entity.ZsjOrg;
import com.ctsi.system.mapper.ZsjOrgMapper;
import com.ctsi.system.service.IZsjOrgService;
import com.ctsi.system.vo.ZsjOrgResVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电信组织管理 服务实现类
 * </p>
 *
 * @author 付雪冬
 * @date 2022-03-01 17:25:49
 */
@Service
public class ZsjOrgServiceImpl extends ServiceImpl<ZsjOrgMapper, ZsjOrg> implements IZsjOrgService {

	@Override
	public IPage<ZsjOrg> listPage(ZsjOrg zsjorg,Search search) {
		boolean isPid = $.isNotEmpty(zsjorg.getPatryOrgId());
		LambdaQueryWrapper<ZsjOrg> queryWrapper = Wrappers.<ZsjOrg>lambdaQuery()
				// 判断pid不为空，则查询
				.eq(isPid, ZsjOrg::getPatryOrgId, zsjorg.getPatryOrgId())
				.or(isPid)
				.like(isPid, ZsjOrg::getOrgIndexId, zsjorg.getPatryOrgId())
				// 根据组织机构名称模糊查询
				.like($.isNotBlank(search.getKeyword()), ZsjOrg::getOrgName, search.getKeyword())
				// 根据sort进行正序排序
				.orderByAsc(ZsjOrg::getOrgLevel);
		return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
	}

	@Override
	public List<ZsjOrgResVO> tree(LoginUser user, Search search) {
		LambdaQueryWrapper<ZsjOrg> queryWrapper = Wrappers.lambdaQuery();

		// 统一封装数据范围的方法
//		queryWrapper = getDataScopeQuery(queryWrapper, user, search);
		if ($.isEmpty(queryWrapper)) {
			return CollectionUtil.newArrayList();
		}
//		queryWrapper.eq(ZsjOrg::getStatusCd, StatusType.ENABLE.getCode());

		return this.list(queryWrapper).stream().map(zsjorg -> {
			ZsjOrgResVO zsjorgRes = new ZsjOrgResVO();
			zsjorgRes.setId(zsjorg.getOrgId());
			zsjorgRes.setParentId($.toLong(zsjorg.getPatryOrgId()));
			zsjorgRes.setTitle(zsjorg.getOrgName());
			zsjorgRes.setValue($.toStr(zsjorg.getOrgId()));
//			zsjorgRes.setWeight(zsjorg.getSort());
			return zsjorgRes;
		}).collect(Collectors.toList());
	}
}
